package com.example.xymarket.httpUtil;

import android.os.Message;

import com.example.xymarket.common.Info;
import com.example.xymarket.po.Result;
import com.example.xymarket.po.ShoppingCart;
import com.example.xymarket.common.HttpCon;
import com.example.xymarket.common.MyHandler;
import com.example.xymarket.common.MyJson;
import com.example.xymarket.common.MyUrl;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CartService {


    MyHandler myHandler;

    public CartService(MyHandler myHandler1)
    {
        myHandler=myHandler1;
    }

    public CartService() { }

    public void getMyCart()  {
        new Thread(new Runnable(){
            @Override
            public void run() {
                Map<String ,String> head=new HashMap<>();
                head.put("Authorization", Info.getMyUserInfo().getToken());
                try {
                    String Json= HttpCon.OKPost(MyUrl.MarketUrl+"shopping_cart/get_my_shopping_cart",null,head,null);
                    Type type = new TypeToken<Result<List<ShoppingCart>>>() {}.getType();
                    Result result=MyJson.getList(Json,type);
                    Integer code= result.getCode();
                    if(code==0)
                    {
                        List<ShoppingCart> CartList= (List<ShoppingCart>) result.getData();
                        Message message=new Message();
                        message.obj=CartList;
                        message.what=1;
                        myHandler.getHandler().sendMessage(message);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public void InsertCart(final Integer id,final Integer goods_id,final Integer num)  {
        new Thread(new Runnable(){
            @Override
            public void run() {
                Map<String ,String> head=new HashMap<>();
                head.put("Authorization", Info.getMyUserInfo().getToken());
                Map<String,Object> map=new HashMap<>();
                map.put("id",id);
                map.put("userId",Info.getMyUserInfo().getId());
                map.put("num",num);
                map.put("goodsId",goods_id);
                String JsonInput=MyJson.MapToJsonString(map);
                try {
                    String Json= HttpCon.JsonPost(MyUrl.MarketUrl+"shopping_cart/insert_shopping_cart",JsonInput,head);
                    Integer code= MyJson.getCode(Json);
                    if(code==0)
                    {
                        Message message=new Message();
                        message.obj=num;
                        message.what=1;
                        myHandler.getHandler().sendMessage(message);
                    }
                    else
                    {
                        Message message=new Message();
                        message.obj="添加失败！";
                        message.what=-1;
                        myHandler.getHandler().sendMessage(message);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
